﻿using Shop.BLL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.MVC.Controllers
{
    public class ProductController : Controller
    {
        BaseManager<Product> productManager=new BaseManager<Product>();
        BaseManager<ProductInfo> productInfoManager=new BaseManager<ProductInfo>();
        BaseManager<Color> colorManager=new BaseManager<Color>();
        BaseManager<Pictrue> pictureManager=new BaseManager<Pictrue>();

        //显示商品页面
        public ActionResult Info(int id)
        {
            var product= productManager.GetEntity(id);  // 商品基本信息

            string where = "[ProductId]=@id";
            SqlParameter[] sp = {new SqlParameter("@id", id)};
            var guiGe= productInfoManager.GetList(where, sp);   //商品规格列表
            ViewBag.GuiGe=guiGe;

            if (guiGe.Count> 0)
            {
                // 获取第一个规格的颜色
                where = "[ProductInfoId]=@id";
                sp = new SqlParameter[] { new SqlParameter("@id", guiGe[0].Id) };
                var colors= colorManager.GetList(where, sp);

                ViewBag.Colors=colors;

                if (colors.Count> 0)
                {
                    where = "[ColorId]=@id";
                    sp = new SqlParameter[] { new SqlParameter("@id", colors[0].Id) };
                    ViewBag.Images=pictureManager.GetList(where, sp);
                }

            }
           

            return View(product);
        }

        //根据规格id获取颜色及图片
        public ActionResult GetColorAndImg(int id)
        {
            var where = "[ProductInfoId]=@id";
            SqlParameter[] sp = { new SqlParameter("@id", id) };
            var colors = colorManager.GetList(where, sp);

            List<Pictrue> Images=new List<Pictrue>();
            if (colors.Count > 0)
            {
                where = "[ColorId]=@id";
                sp = new SqlParameter[] { new SqlParameter("@id", colors[0].Id) };
                Images = pictureManager.GetList(where, sp);
            }

            return Json(new { colors = colors, images = Images },JsonRequestBehavior.AllowGet);
        }
    }
}